<?php
use think\Db;
?>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>附近商家</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link rel="stylesheet" href="__BEIQUAN__/css/iconfont.css" />
		<link rel="stylesheet" href="__BEIQUAN__/css/mui.min.css" />
		<script type="text/javascript" src="__BEIQUAN__/js/mui.min.js" ></script>
		<link rel="stylesheet" href="__BEIQUAN__/css/style.css" />
		<link rel="stylesheet" href="__BEIQUAN__/css/beiq.css" />
		<style>
			 #d_1{display:block;}
			 #d_2{display:none;}
			 #d_3{display:none;}
			 #d_4{display:none;}
		</style>
	</head>
	<body class="fjsj_content">
		<nav class="bottom" id="footer-list">
				<div class="navbox clearfix">
					<a class="" href="{:url('index/index')}">
						<p class="iconfont icon-iconsy2"></p>
						<p class="mui-tab-label">附近商家</p>
					</a>
					<a class="right mui-active" href="{:url('member/index')}">
						<p class="iconfont icon-yonghu"></p>
						<p class="mui-tab-label">个人中心</p>
					</a>
				</div>
		</nav>
		<div class="mui-content fjsj_content">
			<div style="position: fixed;z-index: 1;width: 100%;min-width: 320px;max-width: 640px;margin: 0 auto;">
				<div class="fjsj_souskbox" style="background-color: #ffffff;">
						<form action="{:url('index/index')}" id="myform">
							<input type="search" id="input" name="key" value="{$key}" placeholder="搜索感兴趣的商家" results="5" />
						</form>
				</div>
				<div class="fjsj_navbox" style="background-color: #ffffff;">
					<ul class="fjsj_navlist">
						<li>
							<a onclick="dingw()" class="dwwz">
								<span class="iconfont icon-location"></span>
								<?php if(session('city')){ ?><span><?php echo session('city'); ?></span><?php }else{ ?><span class="dw"></span><?php } ?>	
							</a>				
						</li>
						<li class="paixbox" id="paix">
							<div class="paix"><span> <?php if(session('s_order')=="juli"){ ?>距离优先<?php } ?><?php if(session('s_order')=="zekou"){ ?>折扣优先<?php } ?><?php if(!session('s_order')){ ?>排序<?php } ?></span><span class="iconfont icon-jiantou"></span></div>
						</li>
						<li class="right" id="sx">
							<span class="iconfont icon-shaixuan"></span>
							<span>筛选</span>
						</li>
					</ul>
					<div class="mask" id="id1" style="z-index:-1;"></div>
					<div class="fjsj_nav_zhpx" id="id2" style="display: none;">										
						<ul class="fjsj_paixlist">
							<li id="id4" <?php if(session('s_order')=="juli"){ ?>class="active clearfix"<?php }else{ ?> class="clearfix" <?php } ?> onclick="juli()">距离优先<span class="iconfont icon-gou"></span></li>
							<li id="id5" <?php if(session('s_order')=="zekou"){ ?>class="active clearfix"<?php }else{ ?>  class="clearfix" <?php } ?> onclick="zekou()">折扣优先<span class="iconfont icon-gou"></span></li>
						</ul>					
					</div>
					<div class="fjsj_nav_shaix" id="id6" style="display: none;" >
						
						<div class="item">
							{volist name="agent_category"  id="vo" key="k"}
							<div class="bt" id="h_{$k}"  onclick="Tab('{$k}')" style="width:21%;float:left;text-align: center;margin: 0 auto;height: 1.5rem;line-height: 1.5rem;margin-bottom: .5rem;margin-right: 3%;border-bottom: solid {if $k==1}#0EC3FF{/if} 1px;color:{if $k==1}#0EC3FF{/if}">{$vo.category_name}</div>
							{/volist}
							{volist name="agent_category"  id="vo" key="k"}
							<ul class="fjsj_nav_sxlist clearfix" style="{if $k!=1}display:none{/if};" id="ul_id{$k}">
								<?php 
									$category=category($vo['id']);
									foreach($category as $val){
									?>
									<li onclick="tianjia(this)" onclick="" id="{$val.id}" <?php if($s_duo){ if(in_array($val['id'],$s_duo)){ ?> name="duoxuan" <?php } } ?> ><?php echo $val['category_name']; ?></li>
								<?php } ?>
							</ul>
							{/volist}
						</div>
						<div class="item">
							<div class="bt"  style="width:100%;float:left;"><div style="width:21%;float:left;text-align: center;margin: 0 auto;height: 1.5rem;line-height: 1.5rem;margin-right: 3%;border-bottom: solid #0EC3FF 1px;color:#0EC3FF;">{$public_category.category_name}</div></div>
							<ul class="fjsj_nav_sxlist clearfix">
								<?php 
								$public_category=category($public_category['id']);
								foreach($public_category as $vat){
								?>
								<li onclick="tianjia(this)" id="{$vat.id}" <?php if($s_duo){ if(in_array($vat['id'],$s_duo)){ ?> name="duoxuan" <?php } } ?> ><?php echo $vat['category_name']; ?></li>
								<?php } ?>
							</ul>
						</div>
						<ul class="fjsj_sxbtnbox clearfix">
							<li>
								<a onclick="chongzhi()">重置</a>
							</li>
							<li>
								<a onclick="wancheng()" class="blue">完成</a>
							</li>
						</ul>
					</div>
				</div>
			</div>
			<ul class="fjsj_list" style="padding-top: 6.2rem;">
			</ul>
			<iframe id="geoPage" width=0 height=0 frameborder=0  style="display:none;" scrolling="no"
				src="https://apis.map.qq.com/tools/geolocation?key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77&referer='倍券'">
			</iframe>
			<input type="hidden" id="page" value="0" />
		</div>
	</body>
</html>
{include file="public/footer" /}
<script src="__PUBLIC__/static/js/jquery.min.js"></script>
<script src="__PUBLIC__/static/js/assist.js"></script>
<script src="__PUBLIC__/static/js/help.js "></script>
<script type="text/javascript" src="__INDEX__/js/menudown.js"></script>
<script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js "></script>
<script type="text/javascript">
	var map,geolocation;
	var list=[];
	window.addEventListener('message', function(event) {
						// 接收位置信息
									var loc = event.data;
									window.city = loc.city; //城市
									window.addr = loc.addr; //详细地址
									window.lat = loc.lat; //
									window.lng = loc.lng; //火星坐标 //TODO 实现业务代码逻辑 
									htmldw = window.city;
									$('.dw').html(window.city);
									if (parseInt($("#page").val())==1){
									}else{
										get_data();
									}
									$("#page").val(parseInt($("#page").val()+1));
						}, false);
        function dingw(){
			var address=window.addr;
			location.href = '{:url("index/dingw")}?address='+address;
		}

		function juli(){
			location.href = '{:url("index/index")}?s_order=juli';
		}

		function zekou(){
			location.href = '{:url("index/index")}?s_order=zekou';
		}

		function Tab(num)
		{
			var i;
			for(i=1;i<=4;i++)
			{
					if(i==num){
					document.getElementById("ul_id"+i).style.display="block";
					document.getElementById("h_"+i).style.borderBottom="1px #0EC3FF solid";
					document.getElementById("h_"+i).style.color="#0EC3FF";
				}else{
					document.getElementById("ul_id"+i).style.display="none";
					document.getElementById("h_"+i).style.borderBottom="solid 1px";
					document.getElementById("h_"+i).style.color="#9999b3";
				}
			}
		}
		
	   var i=0;
        function tianjia(self){
			if ($("#"+self.id).attr('name')=="duoxuan") {
				$("#"+self.id).removeAttr('name');
			} else {
				$("#"+self.id).attr('name',"duoxuan")
			}
		}

		function wancheng(){
			var duoxuan = document.getElementsByName("duoxuan");
			var duo="";
			for(var i =0;i<duoxuan.length;i++){
				duo+=duoxuan[i].id+',';
			}
			location.href = '{:url("index/index")}?s_duo='+duo;
		}

		function chongzhi(){
			   $("*[name='duoxuan']").removeAttr('name');
        }



    
	//通过坐标查询
	function get_data(){
		$.ajax({
			type: "post",
			data: {
				longitude: window.lng,
				latitude: window.lat,
				address: window.addr, 
				city: window.city,
				page: parseInt($("#page").val()),
			},
			url: "{:url('index/index/get_agents')}",
			xhrFields: {withCredentials: true},
			crossDomain: true,
			success: function(data) {
				var html = "";
				if(data.list) {
					list=list.concat(data.list);			
					for(var o in list) {
						var agent = list[o];
						var juli = agent.juli;
						var agent_id = agent.id;
						var diz = agent.address;
						var begin_time = ''
						var end_time = '';
						var dk = 0;
						dk = agent.ad_score;
						var sj=100-dk;
						var unit = 'm';
						
						var action = agent['action'];
						if(action == null) {
							action = "暂无";
						}
						if(juli > 1000) {
							juli = juli / 1000;
							unit = 'km';
						}

						var a_city=$('.dwwz  span:eq(1)').text();
						var b_diz=diz.lastIndexOf(a_city);
                        if((diz.length-b_diz-a_city.length)>16){
							a_diz=diz.substring(b_diz+a_city.length,b_diz+a_city.length+16)+"...";
						}else{
							a_diz=diz.substring(b_diz+a_city.length,diz.length);
						}
						
						if(agent.start_time && agent.start_time.length > 0) {
							begin_time = agent.start_time.substr(0, agent.start_time.length - 3);
						} else {
							begin_time = '00:00';
						}
						if(agent.end_time && agent.end_time.length > 0) {
							end_time = agent.end_time.substr(0, agent.end_time.length - 3);
						} else {
							end_time = '00:00';
						}
						
						var logo = agent.logo;
						if(!logo) {
							logo = "__PUBLIC__/static/index/images/noimage.jpg";
						}
						var agent_name = agent.agent_name;
						var str = agent_name.length;
						if(str > 8) {
							agent_name = agent_name.substring(0, 8) + "...";
						}
                        if(agent_id){
							tag_1(agent_id);
							// quan(agent_id);
						}
						var url = "/index/index/map?lng=" + agent.longitude + "&lat=" + agent.latitude;
						html += "<li>" +
							"<a href='/index/index/shop_info?agent_id=" + agent_id + "'>" +
								"<div class='fjsj_img'><img src='" + logo + "'></div>" +
								"<div class='fjsj_infobox'>" +
									"<div class='fjsj_nambox clearfix'>"+
										"<div class='jul'><span class='iconfont icon-qizhi'></span>"+ juli + unit +"</div>"+
										"<div class='nam'>" + agent_name + "</div>" +
									"</div>"+
									"<div class='weizbox'>"+
										"<span class='iconfont icon-location'></span>"+
										"<span class='weiz'>" + a_diz + "</span>"+
									"</div>";
									html +="<div class='fjsj_tipbox' id="+agent_id+">";
											html +="<span style='height:2rem;border-color:#ffffff;'></span>";
										html +="</div>";
										html +="<div class='glj clearfix'>"+
										// "<div class="+agent_id+"></div>"+
										"<div>鼓励金可抵扣</div>"+
										"<div class='jdtbox'>"+
											"<div class='jdt_t' style='width:" + sj + "%;'></div>" +
										"</div>"+
										"<div class='num'>"+dk+"%</div>"+
									"</div>" +
								"</div>" +
							"</a>" +
							"</li>";
					}
					$('.fjsj_list').html(html);
				} else {
					$('.fjsj_list').html("<li style='width:70%;height:200px;margin:20% auto;'><img src='__BEIQUAN__/images/meiyou.png'></li>");
				}
			}
		});
	}

	$(window).scroll(function() {
		var scrollTop = $(window).scrollTop(); //滚动条高度
		var bodyheight = $(document).height(); //文档的高度
		var windowheight = window.innerHeight; //$(window).height();              //当前窗体的高度
		if((bodyheight - windowheight - scrollTop) == 0) {
			var index = layer.load(1, {
				shade: false,
				time: 1.5 * 1000
			});
            $("#page").val(parseInt($("#page").val()) + 1);
			$.ajax({
				type: "post",
				data: {
					longitude: window.lng, //113.015137,113.015137
					latitude:  window.lat,
					address:  window.addr, 
					city:  window.city,
					page: parseInt($("#page").val()),
				},
				
				url: "{:url('index/index/get_agents')}",
				xhrFields: {withCredentials: true},
			    crossDomain: true,
				success: function(data) {
					var html = "";
					if(data.list) {
						list=list.concat(data.list);	
						
						for(var o in list) {
							// for(var i = 0;i < list.length; i++) {
							var agent = list[o];
							var juli = agent.juli;
							var diz = agent.address;
							var agent_id = agent.id;
							var begin_time = ''
							var end_time = '';
							var dk = 0;
							dk = agent.ad_score;
							var sj=100-dk;
							var unit = 'm';
							var action = agent['action'];
							if(action == null) {
								action = "暂无";
							}
							if(juli > 1000) {
								juli = juli / 1000;
								unit = 'km';
							}

							var a_city=$('.dwwz  span:eq(1)').text();
							var b_diz=diz.lastIndexOf(a_city);
							if((diz.length-b_diz-a_city.length)>16){
								a_diz=diz.substring(b_diz+a_city.length,b_diz+a_city.length+16)+"...";
							}else{
								a_diz=diz.substring(b_diz+a_city.length,diz.length);
							}
							
							
							if(agent.start_time && agent.start_time.length > 0) {
								begin_time = agent.start_time.substr(0, agent.start_time.length - 3);
							} else {
								begin_time = '00:00';
							}
							if(agent.end_time && agent.end_time.length > 0) {
								end_time = agent.end_time.substr(0, agent.end_time.length - 3);;
							} else {
								end_time = '00:00';
							}
							var logo = agent.logo;
							if(!logo){
								logo = "__PUBLIC__/static/index/images/noimage.jpg";
							}

							var agent_name = agent.agent_name;
							var str = agent_name.length;
							if(str > 8) {
								agent_name = agent_name.substring(0, 8) + "...";
							}
							if(agent_id){
								tag_1(agent_id);
								// quan(agent_id);
							}
							var url = "/index/index/map?lng=" + agent.longitude + "&lat=" + agent.latitude;
							html += "<li>" +
							"<a href='/index/index/shop_info?agent_id=" + agent_id + "'>" +
								"<div class='fjsj_img'><img src='" + logo + "'></div>" +
								"<div class='fjsj_infobox'>" +
									"<div class='fjsj_nambox clearfix'>"+
										"<div class='jul'><span class='iconfont icon-qizhi'></span>"+ juli + unit +"</div>"+
										"<div class='nam'>" + agent_name + "</div>" +
									"</div>"+
									"<div class='weizbox'>"+
										"<span class='iconfont icon-location'></span>"+
										"<span class='weiz'>" + a_diz + "</span>"+
									"</div>";
									html +="<div class='fjsj_tipbox' id="+agent_id+">";	
											html +="<span style='height:2rem;border-color:#ffffff;'></span>";
										html +="</div>";
										html +="<div class='glj clearfix'>"+
										// "<div class="+agent_id+"></div>"+
										"<div >鼓励金可抵扣</div>"+
										"<div class='jdtbox'>"+
											"<div class='jdt_t' style='width:" + sj + "%;'></div>" +
										"</div>"+
										"<div class='num'>"+dk+"%</div>"+
									"</div>" +
								"</div>" +
							"</a>" +
							"</li>";
						}
						$('.fjsj_list').html(html);
					}
				}
			});
		}
	})

	function tag_1(agent_id) {
		var agent_id=agent_id;
		$.ajax({
			type: "post",
			data: {
				agent_id: agent_id,
			},
			url: "{:url('index/index/agent_tag')}",
			success: function(data) {
				html="";
				if(data.list) {
				var	list=data.list;
						if(list.num>0){
							html+="<span>"+'<img style="width:20px;height:20px;float:right;border-color:#ffffff;" src="__BEIQUAN__/images/17.png">'+"</span>";	
						}
						if(list.tag_1){
							html+="<span>"+list.tag_1+"</span>";
						}
						if(list.tag_2){
							html+="<span>"+list.tag_2+"</span>";	
						}

						if(list.tag_3){
							html+="<span>"+list.tag_3+"</span>";			
						}

						if(list.tag_4){
							html+="<span>"+list.tag_4+"</span>";	
						}
						if(list.tag_5){
							html+="<span>"+list.tag_5+"</span>";		
						}
						if(html==""){
						    $("#"+agent_id).html("<span style='height:2rem;border-color:#ffffff;'></span>");
						}else{
							$("#"+agent_id).html(html);
						}
				} else {
					$("#"+agent_id).html("<span style='height:2rem;border-color:#ffffff;'></span>");
				}
			} 
		});
	}

	// function quan(agent_id) {
	// 	var agent_id=agent_id;
	// 	$.ajax({
	// 		type: "post",
	// 		data: {
	// 			agent_id: agent_id,
	// 		},
	// 		url: "{:url('index/index/agent_quan')}",
	// 		success: function(data) {
	// 			html="";
	// 			if(data.list) {
	// 			var	list=data.list;
	// 					if(list.num>0){
	// 						html+='<img style="width:20px;height:20px;float:right;border-color:#ffffff;" src="__BEIQUAN__/images/17.png">';	
	// 					}
	// 					if(html==""){
	// 					    $("."+agent_id).html("");
	// 					}else{
	// 						$("."+agent_id).html(html);
	// 					}
	// 			} else {
	// 				$("."+agent_id).html("");
	// 			}
	// 		} 
	// 	});
	// }

			var oBtn = document.getElementById('sx'); // 假设按钮的id为btn
			var oDiv = document.getElementById('id6'); // 假设弹出层的id为div
			var omask = document.getElementById('id1');
			var opx = document.getElementById('paix');
			var opDiv = document.getElementById('id2');
			// 点击按钮
			oBtn.onclick = function() {
			    if (oDiv.style.display == 'none') { // 如果层是隐藏的
			        oDiv.style.display = 'block';
			        omask.style.display = 'block';
			        oBtn.className = 'right active';
					opx.className = 'paixbox';
					opDiv.style.display = 'none';
			        omask.style.display = 'none';
			    } else { // 如果层是显示的
			        oDiv.style.display = 'none';
			        omask.style.display = 'none';
			        oBtn.className = 'right';
			    }
			}

			// 点击按钮
			opx.onclick = function() {
			    if (opDiv.style.display == 'none') { // 如果层是隐藏的
			        opDiv.style.display = 'block';
			        omask.style.display = 'block';
					opx.className = 'paixbox active';
					 oDiv.style.display = 'none';
			        omask.style.display = 'none';
			        oBtn.className = 'right';
			    } else { // 如果层是显示的
			        opDiv.style.display = 'none';
			        omask.style.display = 'none';
					opx.className = 'paixbox';
			    }
			}
		</script>
